import json
import time
from queue import Queue
from src.common.logger import getLogger

logger = getLogger()
message_queue = Queue()

def send_message_queue(args):
    logger.info(f"send_message_queue message_queue len: {message_queue.qsize()}")
    message_queue.put(args)

def extract_event_message():
    # 发送初始连接确认
    yield "event: connect\ndata: Connection established\n\n"

    # 生成响应流
    while True:
        if message_queue.qsize() == 0:
            time.sleep(600)  # 控制流速
        message = message_queue.get(timeout = 5)
        yield f"data: {json.dumps(message)}\n\n"

        time.sleep(600)  # 控制流速

    # 发送完成事件
    yield "event: complete\ndata: Streaming finished\n\n"
